package com.sumer.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test1 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()->{ticket.sale();},"A").start();
        new Thread(()->{ticket.sale();},"B").start();
        new Thread(()->{ticket.sale();},"C").start();
        new Thread(()->{ticket.sale();},"D").start();
    }
}

class Ticket{
    private int number = 20;

    Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock();
        try {
            for (int i = 0; i < 20; i++) {
                if (number > 0){
                    number --;
                    System.out.println(number);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}
